<template><div><h2 id="部署" tabindex="-1"><a class="header-anchor" href="#部署"><span>部署</span></a></h2>
<p>Laravel 6 中文文档 /</p>
<p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="部署-1" tabindex="-1"><a class="header-anchor" href="#部署-1"><span>部署</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#server-configuration">服务器配置</a>
<ul>
<li><a href="#nginx">Nginx</a></li>
</ul>
</li>
<li><a href="#optimization">改进</a>
<ul>
<li><a href="#autoloader-optimization">改进自动加载器</a></li>
<li><a href="#optimizing-configuration-loading">改进配置加载</a></li>
<li><a href="#optimizing-route-loading">改进路由加载</a></li>
</ul>
</li>
<li><a href="#deploying-with-forge">使用 Forge 部署</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>当你准备部署你的 Laravel 应用到生产环境时，请确保几个重要的注意点以保证你的应用能尽可能高效的运行。本文中我们将会覆盖几个重点来确保你的 Laravel 应用部署得当。</p>
<h2 id="服务器配置" tabindex="-1"><a class="header-anchor" href="#服务器配置"><span>服务器配置</span></a></h2>
<h3 id="nginx" tabindex="-1"><a class="header-anchor" href="#nginx"><span>Nginx</span></a></h3>
<p>如果你想要部署你的应用到 Nginx 服务器上，你可能会用到下面这个配置文件作为一个开始来配置你的 Web 服务器。很有可能，这个文件需要根据你的服务器配置来做一些自定义的修改。如果你需要协助来管理你的服务器，可以考虑使用 <a href="https://forge.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Forge</a>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">server <span class="token punctuation">{</span></span>
<span class="line">    listen <span class="token number">80</span><span class="token punctuation">;</span></span>
<span class="line">    server_name example<span class="token operator">.</span>com<span class="token punctuation">;</span></span>
<span class="line">    root <span class="token operator">/</span>example<span class="token operator">.</span>com<span class="token operator">/</span><span class="token keyword">public</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    add_header <span class="token constant">X</span><span class="token operator">-</span>Frame<span class="token operator">-</span>Options <span class="token string double-quoted-string">"SAMEORIGIN"</span><span class="token punctuation">;</span></span>
<span class="line">    add_header <span class="token constant">X</span><span class="token operator">-</span><span class="token constant">XSS</span><span class="token operator">-</span>Protection <span class="token string double-quoted-string">"1; mode=block"</span><span class="token punctuation">;</span></span>
<span class="line">    add_header <span class="token constant">X</span><span class="token operator">-</span>Content<span class="token operator">-</span>Type<span class="token operator">-</span>Options <span class="token string double-quoted-string">"nosniff"</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    index index<span class="token operator">.</span>html index<span class="token operator">.</span>htm index<span class="token operator">.</span>php<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    charset utf<span class="token operator">-</span><span class="token number">8</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    location <span class="token operator">/</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name type-declaration">try_files</span> <span class="token variable">$uri</span> <span class="token variable">$uri</span><span class="token operator">/</span> <span class="token operator">/</span>index<span class="token operator">.</span>php<span class="token operator">?</span><span class="token variable">$query_string</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    location <span class="token operator">=</span> <span class="token operator">/</span>favicon<span class="token operator">.</span>ico <span class="token punctuation">{</span> access_log off<span class="token punctuation">;</span> log_not_found off<span class="token punctuation">;</span> <span class="token punctuation">}</span></span>
<span class="line">    location <span class="token operator">=</span> <span class="token operator">/</span>robots<span class="token operator">.</span>txt  <span class="token punctuation">{</span> access_log off<span class="token punctuation">;</span> log_not_found off<span class="token punctuation">;</span> <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    error_page <span class="token number">404</span> <span class="token operator">/</span>index<span class="token operator">.</span>php<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    location <span class="token operator">~</span> \<span class="token operator">.</span><span class="token class-name type-declaration">php</span>$ <span class="token punctuation">{</span></span>
<span class="line">        fastcgi_pass unix<span class="token punctuation">:</span><span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>run<span class="token operator">/</span>php<span class="token operator">/</span>php7<span class="token operator">.</span><span class="token number">2</span><span class="token operator">-</span>fpm<span class="token operator">.</span>sock<span class="token punctuation">;</span></span>
<span class="line">        fastcgi_index index<span class="token operator">.</span>php<span class="token punctuation">;</span></span>
<span class="line">        fastcgi_param <span class="token class-name type-declaration">SCRIPT_FILENAME</span> <span class="token variable">$realpath_root</span><span class="token variable">$fastcgi_script_name</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token keyword">include</span> fastcgi_params<span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    location <span class="token operator">~</span> <span class="token operator">/</span>\<span class="token operator">.</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator">!</span>well<span class="token operator">-</span>known<span class="token punctuation">)</span><span class="token operator">.</span><span class="token operator">*</span> <span class="token punctuation">{</span></span>
<span class="line">        deny all<span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="改进" tabindex="-1"><a class="header-anchor" href="#改进"><span>改进</span></a></h2>
<h3 id="自动加载器改进" tabindex="-1"><a class="header-anchor" href="#自动加载器改进"><span>自动加载器改进</span></a></h3>
<p>当你准备往生产环境部署应用时，确保你优化了你的 Composer 类的自动加载映射，这样可以使 Composer 可以很快的找到正确的加载文件去加载给定的类:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer install <span class="token operator">--</span>optimize<span class="token operator">-</span>autoloader <span class="token operator">--</span>no<span class="token operator">-</span>dev</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>小提示：除了优化自动加载器，你还应该确保在你的项目代码仓库中包含了 composer.lock 这个文件。当你的项目代码中有 composer.lock 这个文件时，便可以更快的安装项目中需要的依赖项。</p>
</blockquote>
<h3 id="优化配置加载" tabindex="-1"><a class="header-anchor" href="#优化配置加载"><span>优化配置加载</span></a></h3>
<p>当你将应用程序部署到生产环境时，你应当确保在你部署过程中运行 <code v-pre>config:cache</code> Artisan 命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan config<span class="token punctuation">:</span>cache</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>此命令将所有 Laravel 的配置文件合并到一个缓存文件，这将极大地减少框架在加载配置值时必须对文件系统进行访问的次数。</p>
<blockquote>
<p>注意：如果在你部署过程中执行 <code v-pre>config:cache</code> 命令，你应当确保你仅从你的配置文件中调用 <code v-pre>env</code> 函数。一旦配置被缓存，<code v-pre>.env</code> 文件将不被加载并且对 <code v-pre>env</code> 函数的所有调用将返回 null。</p>
</blockquote>
<h3 id="优化路由加载" tabindex="-1"><a class="header-anchor" href="#优化路由加载"><span>优化路由加载</span></a></h3>
<p>如果你想构建具有许多路由的大型应用程序，你应当确保在你部署的过程中运行 <code v-pre>route:cache</code> Artisan 命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan route<span class="token punctuation">:</span>cache</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>此命令将为所有路由注册缩减到一个缓存文件中的单个方法调用，从而在注册数百个路由时提高了路由注册的性能。</p>
<blockquote>
<p>注意：由于此功能使用 PHP 序列化，你仅能缓存专门使用基于控制器路由的应用程序路由。PHP 不能序列化闭包路由。</p>
</blockquote>
<h2 id="使用-forge-部署" tabindex="-1"><a class="header-anchor" href="#使用-forge-部署"><span>使用 Forge 部署</span></a></h2>
<p>如果你还没有准备好管理自己的服务器配置，或者不熟悉配置对运行强大的 Laravel 应用程序所需的各种服务, <a href="https://forge.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Forge</a>是一个不错的选择。</p>
<p>Laravel Forge 能在各种基础设施提供商（如：DigitalOcean，Linode，AWS 等等）上创建服务器。另外，Forge 安装和管理构建强大 Laravel 应用程序所需的所有工具，比如：Nginx，MySQL，Redis，Memcached，Beanstalk 等等。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/deployment/5129" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/dep...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/deployment/5129" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/dep...</a></p>
</blockquote>
</div></template>


